package api

import (
	"gitee.com/ling1314/origin-utility/constant"
	"gitee.com/ling1314/origin-utility/model"
	"gitee.com/ling1314/origin-utility/modules/service"
	"github.com/gin-gonic/gin"
)

type deptApi struct{}

func (api deptApi) Add(c *gin.Context) {
	argus := new(model.DeptForm)
	if err := c.ShouldBindJSON(&argus); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	self := GetCurrentUser(c)
	if err := service.DeptService.Add(argus, self); err != nil {
		BadWithCode(c, constant.ErrorUnknown)
		return
	}
	Ok204(c)
}

func (api deptApi) Update(c *gin.Context) {
	argus := new(model.DeptForm)
	if err := c.ShouldBindJSON(&argus); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.DeptService.Update(argus); err != nil {
		BadWithCode(c, constant.ErrorUnknown)
		return
	}
	Ok204(c)
}

func (api deptApi) FindAll(c *gin.Context) {
	argus := new(model.SearchDept)
	if err := c.ShouldBindQuery(&argus); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	result, err := service.DeptService.FindAll(argus)
	if err != nil {
		BadWithCode(c, constant.ErrorUnknown)
		return
	}
	Ok(c, result)
}

func (api deptApi) Delete(c *gin.Context) {
	params := struct {
		DeptId int64 `uri:"id"`
		Del    bool  `uri:"del"`
	}{}
	if err := c.ShouldBindUri(&params); err != nil {
		BadWithCode(c, constant.ParamsError)
		return
	}
	if err := service.DeptService.Delete(params.DeptId, params.Del); err != nil {
		BadWithCode(c, constant.ErrorUnknown)
		return
	}
	Ok204(c)
}
